package com.qujia.station.service.impl;

import com.qujia.common.utils.DateUtils;
import com.qujia.station.domain.NonOilOrder;
import com.qujia.station.domain.OrderDetails;

import com.qujia.station.mapper.NonOilOrderMapper;
import com.qujia.station.mapper.OrderDetailsMapper;
import com.qujia.station.service.NonOilOrderService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * (NonOilOrder)表服务实现类
 *
 * @author makejava
 * @since 2023-08-16 17:46:22
 */
@Service("nonOilOrderService")
public class NonOilOrderServiceImpl implements NonOilOrderService {
    private static final Logger log = LoggerFactory.getLogger(NonOilOrderServiceImpl.class);
    @Resource
    private NonOilOrderMapper nonOilOrderMapper;

    @Resource
    private OrderDetailsMapper orderDetailsMapper;


    /**
     * 分页查询
     *
     * @param nonOilOrder 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<NonOilOrder> queryByPage(NonOilOrder nonOilOrder, PageRequest pageRequest) {
        long total = this.nonOilOrderMapper.count(nonOilOrder);
        return new PageImpl<>(this.nonOilOrderMapper.queryAllByLimit(nonOilOrder, pageRequest), pageRequest, total);
    }

    @Override
    public List<NonOilOrder> query(NonOilOrder nonOilOrder) {
        return nonOilOrderMapper.query(nonOilOrder);
    }

    @Override
    public Map<String, Object> getStatisticsData(NonOilOrder nonOilOrder) {
        return nonOilOrderMapper.getStatisticsData(nonOilOrder);
    }

    @Override
    public Map<String, Object> getTodayStatisticsData(NonOilOrder nonOilOrder) {
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        nonOilOrder.setBeginTime(dateFormat.format(DateUtils.getTodayStartTime()));
        nonOilOrder.setEndTime(dateFormat.format(DateUtils.getTodayEndTime()));
        Map<String, Object> statisticsData = nonOilOrderMapper.getStatisticsData(nonOilOrder);
        if(statisticsData.get("count").equals(0l)){
            statisticsData.put("payableAmountTotal",0);
            statisticsData.put("actuallyAmountTotal",0);
            statisticsData.put("discountAmountTotal",0);
        }
        return statisticsData;
    }

    @Override
    public List<NonOilOrder> queryList(NonOilOrder nonOilOrder) {
        return nonOilOrderMapper.query(nonOilOrder);
    }

    /**
     * 新增数据
     *
     * @param nonOilOrder 实例对象
     * @return 实例对象
     */
    @Override
    public NonOilOrder insert(NonOilOrder nonOilOrder) {
        nonOilOrderMapper.insert(nonOilOrder);
        return nonOilOrder;
    }

    /**
     * 修改数据
     *
     * @param nonOilOrder 实例对象
     * @return 实例对象
     */
    @Override
    public int update(NonOilOrder nonOilOrder) {
        int i = nonOilOrderMapper.update(nonOilOrder);
        return i;
    }

    @Override
    public List<OrderDetails> selectListByOrderCode(String orderCode) {
        return orderDetailsMapper.selectListByOrderCode(orderCode);
    }

}
